<?php
class hrd_periode_model extends MY_Model {
	var $nama_tabel = 'hrd_periode';
	
	function GetJSON() {
		$ls_sql	= "	SELECT	 id_periode_pk, nama_periode, ket_periode, TO_CHAR(tgl_mulai,'DD/MM/YYYY') tgl_mulai, TO_CHAR(tgl_selesai,'DD/MM/YYYY') tgl_selesai
					FROM	".$this->nama_tabel." 
					WHERE	(UPPER(id_periode_pk) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_periode) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(NVL(ket_periode,'')) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'tgl_mulai' : post('sort'))." ".(post('order')=='' ? 'desc' : post('order'));
					
					
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_periode_pk' => $arr_rule['id_periode_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GetSingleView($id_periode_pk) {
		$sql = "select TO_CHAR(tgl_mulai, 'DD') as tgl_mulai, TO_CHAR(tgl_selesai, 'DD') as tgl_selesai, TO_CHAR(tgl_mulai, 'MM') as bulan, TO_CHAR(tgl_mulai, 'YYYY') as tahun
				from hrd_periode 
				where id_periode_pk = '".strtoupper($id_periode_pk)."'
				";

		$result = $this->db->query($sql);


		if($result->num_rows() == 1) {
			return $result->row_array();
		} else {
			return NULL;
		}

	}

}
?>